package org.roger.index.filter;

import java.io.IOException;

//import org.apache.commons.codec.StringEncoder;
//import org.apache.commons.codec.language.Metaphone;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

/**
 * 同音词过滤器
 * @author luoweiyi
 */
public class MetaphoneReplacementFilter extends TokenFilter{
	
	public static final String METAPHONE = "METAPHONE";

	  //private StringEncoder metaphoner = new Metaphone();
	  private CharTermAttribute termAtt;
	  private TypeAttribute typeAtt;

	  public MetaphoneReplacementFilter(TokenStream input) {
		 super(input);
		 termAtt = input.addAttribute(CharTermAttribute.class);
		 typeAtt = input.addAttribute(TypeAttribute.class);
	  }

	@Override
	public boolean incrementToken() throws IOException {
		clearAttributes();
		if(!input.incrementToken()){
			return false;
		}
		
		try {
			//将语汇单元转换成音素
			String encoded = "";//metaphoner.encode(termAtt.toString());
			termAtt.setEmpty();
			termAtt.append(encoded);
			typeAtt.setType(METAPHONE);
			System.out.println(encoded);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return true;
	}
}
